package com.csust.automaticteach.app.tools;

/**
 * @ClassNameFileOprationTool
 * @Description TODO
 * @Author钰玟
 * @Date2025/6/14 下午2:51
 * @Version 1.0
 **/

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import static com.csust.automaticteach.constant.ToolsConstant.FILE_PATH;

/**
 * 文件读取工具类
 */
public class FileOperationTool {
    @Tool(description = "Read content from a file")
    public String readFile(@ToolParam(description = "the fileName to read") String fileName) {
        String filePath = FILE_PATH + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (IORuntimeException e) {
            return "fail to read the file" + e.getMessage();
        }
    }

    @Tool(description = "Write content to a file")
    public String writeFile(@ToolParam(description = "the fileName to write") String fileName,
                            @ToolParam(description = "the content to write") String content
    ) {
        String filePath = FILE_PATH + "/" + fileName;
        try {
            FileUtil.mkdir(FILE_PATH);
            FileUtil.writeUtf8String(content, filePath);
            return "success to write the file" + filePath;
        } catch (IORuntimeException e) {
            return "fail to write the file" + e.getMessage();
        }
    }

}
